#ifndef CUSTOMGRAPHICSRECT_H
#define CUSTOMGRAPHICSRECT_H

#include <QGraphicsRectItem>
#include <QObject>
#include <QRect>
#include <QPaintEvent>
#include <QGraphicsScene>
#include <QDebug>
#include <QtMath>
#include <QGraphicsSceneMouseEvent>
#include <QCursor>

class CustomGraphicsRect : public QGraphicsRectItem
{

//    QPointF _points[4] = {
//        QPointF(10,10),
//        QPointF(50,10),
//        QPointF(50,50),
//        QPointF(10,50),
//    };

    QPointF _points[4] = {
        QPointF(150,150),
        QPointF(550,150),
        QPointF(550,550),
        QPointF(150,550),
    };

//QPointF _points[4] = {
//    QPointF(0,0),
//    QPointF(400,0),
//    QPointF(400,400),
//    QPointF(0,400),
//};

    enum MOUSE_STATUS{
        NORMAL,  //正常状态
        DRAGA_LEFTTOP,//拖动左上角
        DRAGA_RIGHTTOP,//拖动右上角
        DRAGA_RIGHTBOTTOM,//拖动右下角
        DRAGA_LEFTBOTTOM//拖动左下角
    };

public:
    CustomGraphicsRect();
    ~CustomGraphicsRect();


    void move(float x,float y);//移动
    void setAngle(float val);//设置旋转角度
    QPointF getBottomright();//获取右下角坐标
    qreal getAngle();
    void resize(QPointF moveP,QPointF staticP,int indexMove);//根据新右下角坐标调整矩形大小
    void rotate(qreal angle);//旋转
    QRectF boundingRect() const override;


    QPolygonF _rect;//多边形（矩形）
protected:
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);     //绘制事件

    void mousePressEvent(QGraphicsSceneMouseEvent *event) override;       //鼠标点击
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;        //鼠标移动
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;     //鼠标松开


private:
    void init_old_rect(QPolygonF ploy);
    QPointF get_rotate_point(QPointF center_point, QPointF in_point, qreal angle);//旋转一个点
    //QList<QPointF> get_rotate_points(QPointF center_point, QList<QPointF> in_points, qreal angle);


    QPolygonF rb_rect_polygon;      //右下点击区域
    QRectF rb_rect;

    QPolygonF lt_rect_polygon;      //左上点击区域
    QRectF lt_rect;

    QPolygonF lb_rect_polygon;      //左下点击区域
    QRectF lb_rect;
private:
    MOUSE_STATUS _mouseStatus = NORMAL;

    QPointF _startPoint;//拖动起点
//    qreal _width;//宽
//    qreal _height;//高
    qreal _angle = 0;//度数

};

#endif // CUSTOMGRAPHICSRECT_H
